package com.baofu.paycore.manager.converter;

import com.alibaba.fastjson.JSON;
import com.baofu.paycore.common.enums.FundTypeEnum;
import com.baofu.paycore.common.enums.UpdateUser;
import com.baofu.paycore.common.enums.UsableFlagEnum;
import com.baofu.paycore.common.utils.Constant;
import com.baofu.paycore.dal.model.*;
import com.baofu.paycore.manager.model.*;
import com.baofu.paycore.manager.model.preAuth.PreAuthBO;
import com.baofu.paycore.manager.model.preAuth.SubPreAuthBO;
import com.baofu.paycore.manager.model.withdraw.WithdrawBO;
import com.baofu.paycore.manager.model.withdraw.WithdrawBankBO;
import com.baofu.paycore.manager.model.withdraw.WithdrawFundBO;
import com.baofu.paycore.service.facade.enums.PayStatusEnums;
import com.baofu.paycore.service.facade.enums.PayToolTypeEnum;
import com.baofu.paycore.service.facade.enums.RequestSystemEnum;
import com.baofu.paycore.service.facade.enums.ServiceCodeEnum;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * BO转换成DTO对象
 * <p>
 *     1、充值接口返回结果信息处理
 * </p>
 * User: kwl  Date: 16/5/7 ProjectName: paycore Version: 5.0.0
 */
public class BO2DOConverter {

    /**
     * 充值请求对象转换充值主单对象
     *
     * @param rechargeBO    充值请求对象BO
     * @return              充值主单对象DO
     */
    public static ClearDO getClearDO(RechargeBO rechargeBO){
        ClearDO clearDO = new ClearDO();
        if(null == rechargeBO){
            return clearDO;
        }

        clearDO.setPayNo(rechargeBO.getPayNo());
        clearDO.setRelationNo(rechargeBO.getRelationNo());
        clearDO.setRequestOrderNo(rechargeBO.getRequestOrderNo());
        clearDO.setRequestSystem(rechargeBO.getRequestSystem());
        clearDO.setRequestChannel(rechargeBO.getRequestChannel());
        clearDO.setRequestBizNo(rechargeBO.getRequestBizNo());
        clearDO.setRequestDate(rechargeBO.getRequestDate());

        clearDO.setAccountNo(rechargeBO.getAccountNo());
        clearDO.setBizType(rechargeBO.getBizType());
        clearDO.setSubBizType(rechargeBO.getSubBizType());

        clearDO.setCcy(rechargeBO.getCcy());
        clearDO.setAmt(rechargeBO.getAmt());
        clearDO.setStatus(rechargeBO.getStatus());

        clearDO.setServiceCode(rechargeBO.getServiceCode());
        clearDO.setMerchantNo(rechargeBO.getMerchantNo());
        clearDO.setContractNo(rechargeBO.getContractNo());
        clearDO.setPaymentInfo(rechargeBO.getPaymentInfo());
        clearDO.setDepositExtra(JSON.toJSONString(rechargeBO.getPaymentExt()));
        clearDO.setDescription(rechargeBO.getDescription());

        clearDO.setCreatedBy(rechargeBO.getOperator());
        clearDO.setUpdatedBy(rechargeBO.getOperator());
        return clearDO;
    }

    /**
     * 封装提现单
     *
     * @param withdrawBO    提现参数对象
     * @return              提现主单
     */
    public static ClearDO getWithdrawClearDO(WithdrawBO withdrawBO){
        ClearDO clearDO = new ClearDO();
        clearDO.setUpdatedBy(StringUtils.isEmpty(withdrawBO.getOperator()) ? UpdateUser.SYS.getUserName() :
                withdrawBO.getOperator());
        clearDO.setCreatedBy(withdrawBO.getOperator());
        clearDO.setRelationNo(withdrawBO.getRelationNo());
        clearDO.setRequestBizNo(withdrawBO.getRequestBizNo());
        clearDO.setRequestOrderNo(withdrawBO.getRequestOrderNo());
        clearDO.setPayNo(withdrawBO.getPayNo());
        clearDO.setRequestDate(withdrawBO.getRequestDate());
        clearDO.setAccountNo(withdrawBO.getAccountNo());
        clearDO.setAmt(withdrawBO.getAmt());
        clearDO.setRequestSystem(withdrawBO.getRequestSystem());
        clearDO.setRequestChannel(withdrawBO.getRequestChannel());
        clearDO.setCcy(withdrawBO.getCcy());
        clearDO.setBizType(withdrawBO.getBizType());
        clearDO.setSubBizType(withdrawBO.getSubBizType());
        clearDO.setDescription(withdrawBO.getDescription());
        clearDO.setContractNo(withdrawBO.getContractNo());
        clearDO.setMerchantNo(withdrawBO.getMerchantNo());
        clearDO.setServiceCode(withdrawBO.getServiceCode());
        clearDO.setStatus(withdrawBO.getStatus());
        clearDO.setOldPayNo(withdrawBO.getOldPayNo());
        return clearDO;
    }

    /**
     * 封装提现银行单
     *
     * @param withdrawBO    提现参数对象
     * @return              提现主单
     */
    public static ClearBankDO getClearBankDO(WithdrawBO withdrawBO){

        if (withdrawBO == null){
            return null;
        }
        ClearBankDO clearBankDO = new ClearBankDO();
        WithdrawBankBO withdrawBankBO = withdrawBO.getWithdrawBankBO();
        if(withdrawBankBO!=null){
            clearBankDO.setBankCode(withdrawBankBO.getBankCode());
            //请求渠道 渠道细类
            clearBankDO.setFundChannel(withdrawBankBO.getFundChannel());
            clearBankDO.setSubFundChannel(withdrawBankBO.getFundSubChannel());
            clearBankDO.setUpdatedBy(StringUtils.isEmpty(withdrawBO.getOperator()) ? UpdateUser.SYS.getUserName() :
                    withdrawBO.getOperator());
            clearBankDO.setCreatedBy(withdrawBO.getOperator());
            clearBankDO.setFundDetailNo(withdrawBankBO.getFundDetailNo());
            clearBankDO.setPayNo(withdrawBankBO.getPayNo());
            clearBankDO.setPayToolType(withdrawBankBO.getPayToolType());
            clearBankDO.setPayToolSubType(withdrawBankBO.getPayToolSubType());
            clearBankDO.setAmt(withdrawBankBO.getAmt());
            clearBankDO.setCcy(withdrawBankBO.getCcy());
            clearBankDO.setStatus(withdrawBankBO.getStatus());
            clearBankDO.setTransDate(withdrawBankBO.getTransDate());
            clearBankDO.setTransReceiptNo(withdrawBankBO.getTransReceiptNo());
            clearBankDO.setTransReceiptNo(withdrawBankBO.getClearNo());
            clearBankDO.setErrorCode(withdrawBankBO.getErrorCode());
            clearBankDO.setErrorReason(withdrawBankBO.getErrorReason());
            clearBankDO.setAgreementNo(withdrawBankBO.getAgreementNo());
            clearBankDO.setContractNo(withdrawBankBO.getContractNo());
            clearBankDO.setAgreeRelId(withdrawBankBO.getAgreeRelId());
            clearBankDO.setDescription(withdrawBankBO.getDescription());
        }

        return clearBankDO;
    }

    /**
     * 获取提现账户资金单集合
     *
     * @param withdrawFundBOs   提现单
     * @return                  资金单集合
     */
    public static List<ClearAccountDO> getClearAccountList(List<WithdrawFundBO> withdrawFundBOs, String operator) {
        List<ClearAccountDO> list = new ArrayList<>();
        if (CollectionUtils.isEmpty(withdrawFundBOs)) {
            return list;
        }
        withdrawFundBOs.forEach(withdrawFundBO -> {
            if (!FundTypeEnum.WITHDRAW_FREEZE.getCode().equals(withdrawFundBO.getFundType())) {
                list.add(getClearAccountDO(withdrawFundBO, operator));
            }
        });
        return list;
    }

    /**
     * 提现账户资金单
     *
     * @param withdrawFundBO    提现资金单参数
     * @return                  提现银行单
     */
    public static ClearAccountDO getClearAccountDO(WithdrawFundBO withdrawFundBO, String operator){

        ClearAccountDO clearAccountDO = new ClearAccountDO();
        clearAccountDO.setEventNo(withdrawFundBO.getEventNo());
        clearAccountDO.setFundChannel(withdrawFundBO.getFundChannel());
        clearAccountDO.setSubFundChannel(withdrawFundBO.getFundSubChannel());
        clearAccountDO.setFundDetailNo(withdrawFundBO.getFundDetailNo());
        clearAccountDO.setPayNo(withdrawFundBO.getPayNo());
        clearAccountDO.setPayToolType(withdrawFundBO.getPayToolType());
        clearAccountDO.setPayToolSubType(withdrawFundBO.getPayToolSubType());
        clearAccountDO.setAmt(withdrawFundBO.getAmt());
        clearAccountDO.setCcy(withdrawFundBO.getCcy());
        clearAccountDO.setStatus(withdrawFundBO.getStatus());
        clearAccountDO.setAccountNo(withdrawFundBO.getAccountNo());
        clearAccountDO.setTransDate(withdrawFundBO.getTransDate());
        clearAccountDO.setErrorReason(withdrawFundBO.getErrorReason());
        clearAccountDO.setErrorCode(withdrawFundBO.getErrorCode());
        clearAccountDO.setTransReceiptNo(withdrawFundBO.getTransReceiptNo());
        clearAccountDO.setBizFeeAccount(withdrawFundBO.getBizFeeAccount());
        clearAccountDO.setReqAccAmt(withdrawFundBO.getReqAccAmt());
        clearAccountDO.setFundType(withdrawFundBO.getFundType());
        clearAccountDO.setFreezeCode(withdrawFundBO.getFreezeCode());
        clearAccountDO.setUnfreezeNo(withdrawFundBO.getUnfreezeNo());
        clearAccountDO.setCreatedBy(operator);
        clearAccountDO.setUpdatedBy(StringUtils.isEmpty(operator) ? UpdateUser.SYS.getUserName() : operator);

        return clearAccountDO;
    }

    /**
     * 提现账户资金单
     *
     * @param withdrawFundBO    提现资金单参数
     * @return                  提现银行单
     */
    public static FreezeDO getFreezeDO(WithdrawBO withdrawBO,WithdrawFundBO withdrawFundBO, String operator){

        FreezeDO freezeDO = new FreezeDO();

        freezeDO.setRelationNo(withdrawBO.getRelationNo());
        freezeDO.setRequestSystem(withdrawBO.getRequestSystem());
        freezeDO.setRequestBizNo(withdrawBO.getRequestBizNo());
        freezeDO.setRequestOrderNo(withdrawBO.getRequestOrderNo());
        freezeDO.setRequestDate(withdrawBO.getRequestDate());
        freezeDO.setFreezeNo(withdrawFundBO.getFundDetailNo());
        freezeDO.setPayNo(withdrawFundBO.getPayNo());
        freezeDO.setOrigPayNo(withdrawBO.getPayNo());
        freezeDO.setBizType(withdrawBO.getBizType());
        freezeDO.setSubBizType(withdrawBO.getSubBizType());
        freezeDO.setRequestDate(new Date());

        freezeDO.setMerchantNo(withdrawBO.getMerchantNo());
        freezeDO.setServiceCode(ServiceCodeEnum.UNFREEZE.UNFREEZE.getCode());

        freezeDO.setCcy(withdrawFundBO.getCcy());
        freezeDO.setAmt(withdrawFundBO.getAmt());
        freezeDO.setDescription(withdrawBO.getDescription());
        freezeDO.setStatus(withdrawFundBO.getStatus());
        freezeDO.setAccountNo(withdrawBO.getAccountNo());
        freezeDO.setFreezeType("DRAW");
        freezeDO.setUpdatedBy(StringUtils.isEmpty(operator) ? UpdateUser.SYS.getUserName() : operator);
        freezeDO.setCreatedBy(operator);
        freezeDO.setErrorCode(withdrawFundBO.getErrorCode());
        freezeDO.setErrorReason(withdrawFundBO.getErrorReason());
        freezeDO.setFreezeCode(withdrawFundBO.getFreezeCode());

        return freezeDO;
    }



    /**
     * 退款请求对象转换退款主单对象
     *
     * @param refundBO    退款请求对象BO
     * @return            退款请求主单对象BO
     */
    public static RefundDO getRefundDO(RefundBO refundBO){

        RefundDO refundDO = new RefundDO();

        refundDO.setPayNo(refundBO.getPayNo());
        refundDO.setOrigPayNo(refundBO.getOrigPayNo());
        refundDO.setRelationNo(refundBO.getRelationNo());
        refundDO.setRequestOrderNo(refundBO.getRequestOrderNo());
        refundDO.setRequestSystem(refundBO.getRequestSystem());
        refundDO.setRequestBizNo(refundBO.getRequestBizNo());
        refundDO.setRequestDate(refundBO.getRequestDate());
        refundDO.setRequestChannel(refundBO.getRequestChannel());
        refundDO.setServiceCode(refundBO.getServiceCode());
        refundDO.setBizType(refundBO.getBizType());
        refundDO.setSubBizType(refundBO.getSubBizType());

        refundDO.setCcy(refundBO.getCcy());
        refundDO.setAmt(refundBO.getAmt());
        refundDO.setFeeAmt(refundBO.getFeeAmt());
        refundDO.setRefundFeeAmt(refundBO.getRefundFeeAmt());

        refundDO.setStatus(refundBO.getStatus());
        refundDO.setDescription(refundBO.getDescription());
        refundDO.setRefundExtra(refundBO.getRefundExtra());
        refundDO.setCreatedBy(refundBO.getOperator());
        refundDO.setUpdatedBy(refundBO.getOperator());
        return refundDO;
    }

    /**
     * 退款请求对象转换退款主单对象
     *
     * @param subRefundBO    退款资金单明细
     * @return               退款请求主单对象BO
     */
    public static SubRefundDO getSubRefundDO(SubRefundBO subRefundBO){

        SubRefundDO subRefundDO = new SubRefundDO();

        subRefundDO.setPayNo(subRefundBO.getPayNo());
        subRefundDO.setFundDetailNo(subRefundBO.getFundDetailNo());
        subRefundDO.setFundType(subRefundBO.getFundType());
        subRefundDO.setClassType(subRefundBO.getClassType());

        subRefundDO.setPayToolType(subRefundBO.getPayToolType());
        subRefundDO.setPayToolSubType(subRefundBO.getPayToolSubType());
        subRefundDO.setFundingChannel(subRefundBO.getFundingChannel());
        subRefundDO.setSubFundingChannel(subRefundBO.getSubFundingChannel());
        subRefundDO.setFundBelong(subRefundBO.getFundBelong());

        subRefundDO.setPayerAccNo(subRefundBO.getPayerAccNo());
        subRefundDO.setPayeeAccNo(subRefundBO.getPayeeAccNo());
        subRefundDO.setAmt(subRefundBO.getAmt());
        subRefundDO.setCcy(subRefundBO.getCcy());
        subRefundDO.setStatus(subRefundBO.getStatus());

        subRefundDO.setEventNo(subRefundBO.getEventNo());
        subRefundDO.setTransReceiptNo(subRefundBO.getTransReceiptNo());
        subRefundDO.setTransDate(subRefundBO.getTransDate());
        subRefundDO.setErrorReason(subRefundBO.getErrorMsg());
        subRefundDO.setErrorCode(subRefundBO.getErrorCode());

        subRefundDO.setRelateFundDetailNo(subRefundBO.getRelateFundDetailNo());
        subRefundDO.setOrigTransReceiptNo(subRefundBO.getOrigTransReceiptNo());
        subRefundDO.setOrigTransDate(subRefundBO.getOrigTransDate());
        subRefundDO.setOrigFundDetailNo(subRefundBO.getOrigRequestBizNo());

        subRefundDO.setFdDtlExtra(subRefundBO.getFdDtlExtra());
        subRefundDO.setDescription(subRefundBO.getDescription());
        subRefundDO.setCreatedBy(subRefundBO.getOperator());
        subRefundDO.setUpdatedBy(subRefundBO.getOperator());
        return subRefundDO;
    }

    /**
     * 支付BO对象转换DO对象
     *
     * @param payBO     支付BO对象
     * @return          支付DO对象
     */
    public static PayDO payBOToDO(PayBO payBO) {

        if (payBO == null) {
            return null;
        }
        PayDO payDO = new PayDO();
        payDO.setEventNo(payBO.getEventNo());
        payDO.setPayerNo(payBO.getPayerNo());
        payDO.setPayeeNo(payBO.getPayeeNo());
        payDO.setRefundAmt(payBO.getRefundAmt());
        payDO.setStatus(payBO.getStatus());
        payDO.setPayNo(payBO.getPayNo());
        payDO.setRelationNo(payBO.getRelationNo());
        payDO.setRequestBizNo(payBO.getRequestBizNo());
        payDO.setRequestSystem(payBO.getRequestSystem());
        payDO.setRequestOrderNo(payBO.getRequestOrderNo());
        payDO.setRequestDate(payBO.getRequestDate());
        payDO.setRequestChannel(payBO.getRequestChannel());
        payDO.setBizType(payBO.getBizType());
        payDO.setSubBizType(payBO.getSubBizType());
        payDO.setAmt(payBO.getAmt());
        payDO.setCcy(payBO.getCcy());
        payDO.setRate(payBO.getRate());
        payDO.setSourceAmt(payBO.getSourceAmt());
        payDO.setSourceCcy(payBO.getSourceCcy());
        if(!CollectionUtils.isEmpty(payBO.getPaymentExt())){
            payDO.setPaymentExtra(JSON.toJSONString(payBO.getPaymentExt()));
        }
        payDO.setPaymentInfo(payBO.getPaymentInfo());
        payDO.setDescription(payBO.getDescription());
        payDO.setMerchantNo(payBO.getMerchantNo());
        payDO.setCreatedBy(payBO.getOperator());
        payDO.setUpdatedBy(payBO.getOperator());

        return payDO;
    }

    /**
     * 支付子单对象转换
     *
     * @param subPayBO  子单BO对象
     * @param subPayDO  子单DO对象
     */
    public static void subPayBOToDO(SubPayBO subPayBO, SubPayDO subPayDO){

        subPayDO.setFundDetailNo(subPayBO.getFundDetailNo());
        subPayDO.setPayToolType(subPayBO.getPayToolType());
        subPayDO.setPayToolSubType(subPayBO.getPayToolSubType());
        subPayDO.setClassType(subPayBO.getClassType());
        subPayDO.setPayerAccNo(subPayBO.getPayerAccNo());
        subPayDO.setPayeeAccNo(subPayBO.getPayeeAccNo());
        subPayDO.setCcy(subPayBO.getCcy());
        subPayDO.setPayToolAmt(subPayBO.getPayToolAmt());
        subPayDO.setFundAmt(subPayBO.getFundAmt());
        subPayDO.setRefundAmount(subPayBO.getRefundAmount());
        subPayDO.setStatus(subPayBO.getStatus());
        subPayDO.setFundChannel(subPayBO.getFundingChannel());
        subPayDO.setSubFundChannel(subPayBO.getSubFundingChannel());
        subPayDO.setFreezeCode(subPayBO.getFreezeCode());
        subPayDO.setEventNo(subPayBO.getEventNo());
        subPayDO.setTransDate(subPayBO.getTransDate());
        subPayDO.setTransReceiptNo(subPayBO.getTransReceiptNo());
        subPayDO.setErrorCode(subPayBO.getErrorCode());
        subPayDO.setErrorReason(subPayBO.getErrorReason());
        subPayDO.setFdDtlExtra(subPayBO.getFdDtlExtra());
        subPayDO.setFreezeNo(subPayBO.getFreezeNo());
        subPayDO.setFreezeCode(subPayBO.getFreezeCode());
        subPayDO.setChargeFreezeCode(subPayBO.getChargeFreezeCode());
        subPayDO.setDescription(subPayBO.getDescription());
        subPayDO.setFundType(subPayBO.getFundType());
        subPayDO.setRelateFundDetailNo(subPayBO.getRelateFundDetailNo());
    }

    /**
     * 充值银行单BO转换DO
     *
     * @param rechargeBankBO    充值银行单BO
     * @return                  充值银行单DO
     */
    public static ClearBankDO getClearBankDO(RechargeBankBO rechargeBankBO){
        ClearBankDO clearBankDO = new ClearBankDO();
        //请求渠道 渠道细类
        clearBankDO.setFundDetailNo(rechargeBankBO.getFundDetailNo());
        clearBankDO.setPayNo(rechargeBankBO.getPayNo());
        clearBankDO.setPayToolType(rechargeBankBO.getPayToolType());
        clearBankDO.setPayToolSubType(rechargeBankBO.getPayToolSubType());
        clearBankDO.setCcy(rechargeBankBO.getCcy());
        clearBankDO.setAmt(rechargeBankBO.getAmt());
        clearBankDO.setChargeFee(rechargeBankBO.getChargeFee());
        clearBankDO.setStatus(rechargeBankBO.getStatus());

        clearBankDO.setContractNo(rechargeBankBO.getContractNo());
        clearBankDO.setAgreementNo(rechargeBankBO.getAgreementNo());
        clearBankDO.setAgreeRelId(rechargeBankBO.getAgreeRelId());
        clearBankDO.setBankCode(rechargeBankBO.getBankCode());
        clearBankDO.setCardType(rechargeBankBO.getCardType());
        clearBankDO.setOpFlag(rechargeBankBO.getOpFlag());

        clearBankDO.setFundChannel(rechargeBankBO.getFundChannel());
        clearBankDO.setSubFundChannel(rechargeBankBO.getSubFundChannel());

        clearBankDO.setErrorCode(rechargeBankBO.getErrorCode());
        clearBankDO.setErrorReason(rechargeBankBO.getErrorReason());
        clearBankDO.setTransReceiptNo(rechargeBankBO.getTransReceiptNo());
        clearBankDO.setTransDate(rechargeBankBO.getTransDate());

        clearBankDO.setDescription(rechargeBankBO.getDescription());

        return clearBankDO;
    }

    /**
     * 充值/提现账务单BO转换DO
     *
     * @param rechargeFundBO    账务单Bo
     * @return                  账务单DO
     */
    public static ClearAccountDO getClearAccountDO(RechargeFundBO rechargeFundBO) {

        if (rechargeFundBO == null) {
            return null;
        }
        ClearAccountDO clearAccountDO = new ClearAccountDO();

        clearAccountDO.setPayNo(rechargeFundBO.getPayNo());
        clearAccountDO.setFundType(rechargeFundBO.getFundType().getCode());
        clearAccountDO.setFundDetailNo(rechargeFundBO.getFundDetailNo());
        clearAccountDO.setPayToolType(rechargeFundBO.getPayToolType());
        clearAccountDO.setPayToolSubType(rechargeFundBO.getPayToolSubType());

        clearAccountDO.setCcy(rechargeFundBO.getCcy());
        clearAccountDO.setAmt(rechargeFundBO.getAmt());
        clearAccountDO.setReqAccAmt(rechargeFundBO.getReqAccAmt());
        clearAccountDO.setStatus(rechargeFundBO.getStatus());

        clearAccountDO.setAccountNo(rechargeFundBO.getAccountNo());
        clearAccountDO.setBizFeeAccount(rechargeFundBO.getBizFeeAccount());
        clearAccountDO.setFundChannel(rechargeFundBO.getFundChannel());
        clearAccountDO.setEventNo(rechargeFundBO.getEventNo());
        clearAccountDO.setFundChannel(rechargeFundBO.getFundChannel());
        clearAccountDO.setSubFundChannel(rechargeFundBO.getFundSubChannel());
        clearAccountDO.setTransDate(rechargeFundBO.getTransDate());
        clearAccountDO.setErrorCode(rechargeFundBO.getErrorCode());
        clearAccountDO.setErrorReason(rechargeFundBO.getErrorReason());
        clearAccountDO.setTransReceiptNo(rechargeFundBO.getTransReceiptNo());

        return clearAccountDO;
    }

    /**
     * 获取转账DO对象
     *
     * @param transferBO    转账BO对象
     * @return              转账DO对象
     */
    public static TransferDO getTransferDO(TransferBO transferBO) {
        TransferDO transferDO = new TransferDO();
        transferDO.setRelationNo(transferBO.getRelationNo());
        transferDO.setEventNo(transferBO.getEventNo());
        transferDO.setStatus(transferBO.getStatus());
        transferDO.setBizChargeAmt(transferBO.getBizChargeAmt());
        transferDO.setBizFeeAccount(transferBO.getBizFeeAccount());
        transferDO.setCreatedBy(transferBO.getOperator());
        transferDO.setUpdatedBy(transferBO.getOperator());
        transferDO.setPayNo(transferBO.getPayNo());
        transferDO.setBizType(transferBO.getBizType());
        transferDO.setSubBizType(transferBO.getSubBizType());
        transferDO.setRequestBizNo(transferBO.getRequestBizNo());
        transferDO.setServiceCode(transferBO.getServiceCode());
        transferDO.setRequestOrderNo(transferBO.getRequestOrderNo());
        transferDO.setRequestDate(transferBO.getRequestDate());
        transferDO.setAmt(Constant.validateLong(transferBO.getAmt()));
        transferDO.setCcy(transferBO.getCcy());
        transferDO.setRequestChannel(transferBO.getRequestChannel());
        transferDO.setRequestSystem(transferBO.getRequestSystem());
        transferDO.setDescription(transferBO.getDescription());
        transferDO.setOldPayNo(transferBO.getOldPayNo());
        return transferDO;
    }

    /**
     * 获取提现资金单
     *
     * @param withdrawFundBO     提现资金单
     * @return                  ClearAccountDO
     */
    public static ClearAccountDO getClearAccountByFundBO(WithdrawFundBO withdrawFundBO){

        ClearAccountDO clearAccountDO = new ClearAccountDO();
        clearAccountDO.setFundDetailNo(withdrawFundBO.getFundDetailNo());
        clearAccountDO.setUpdatedBy(UpdateUser.SYS.getUserName());
        clearAccountDO.setStatus(withdrawFundBO.getStatus());
        clearAccountDO.setTransDate(withdrawFundBO.getTransDate());
        clearAccountDO.setTransReceiptNo(withdrawFundBO.getTransReceiptNo());
        clearAccountDO.setErrorCode(withdrawFundBO.getErrorCode());
        clearAccountDO.setErrorReason(withdrawFundBO.getErrorReason());

        return clearAccountDO;
    }

    /**
     * 组装退款单信息
     *
     * @param withdrawBO        提现单
     * @param withdrawFundBO    提现资金单
     * @return                  退款单
     */
    public static RefundDO getRefundDOByWithdrawBO(WithdrawBO withdrawBO, WithdrawFundBO withdrawFundBO){

        RefundDO refundDO = new RefundDO();
        refundDO.setStatus(PayStatusEnums.INIT.getCode());
        refundDO.setRelationNo(withdrawBO.getRelationNo());
        refundDO.setRequestChannel(withdrawBO.getRequestChannel());
        refundDO.setRequestDate(new Date());
        refundDO.setRequestSystem(RequestSystemEnum.PAYCORE.getCode());
        refundDO.setRequestOrderNo(withdrawBO.getPayNo());
        refundDO.setRequestBizNo(withdrawFundBO.getFundDetailNo());
        refundDO.setServiceCode(ServiceCodeEnum.CANCEL.getCode());
        refundDO.setCcy(withdrawBO.getCcy());
        refundDO.setAmt(withdrawBO.getAmt());
        refundDO.setBizType(withdrawBO.getBizType());
        refundDO.setSubBizType(withdrawBO.getSubBizType());
        refundDO.setCreatedBy(withdrawBO.getOperator());
        refundDO.setUpdatedBy(withdrawBO.getUpdatedBy());
        refundDO.setOrigPayNo(withdrawBO.getPayNo());

        return refundDO;
    }

    /**
     * 组装退款子单信息
     *
     * @param withdrawFundBO    提现子单
     * @return                  退款子单
     */
    public static SubRefundDO getSubRefundDOBySubWithdraw(WithdrawFundBO withdrawFundBO){
        SubRefundDO subRefundDO = new SubRefundDO();
        subRefundDO.setFundType(withdrawFundBO.getFundType());
        subRefundDO.setPayToolType(PayToolTypeEnum.ACCOUNT.getCode());
        subRefundDO.setPayToolSubType(withdrawFundBO.getPayToolSubType());
        subRefundDO.setClassType(PayToolTypeEnum.ACCOUNT.getCode());
        subRefundDO.setRelateFundDetailNo(withdrawFundBO.getFundDetailNo());
        subRefundDO.setEventNo(withdrawFundBO.getEventNo());
        subRefundDO.setFundingChannel(withdrawFundBO.getFundChannel());
        subRefundDO.setSubFundingChannel(withdrawFundBO.getFundSubChannel());
        subRefundDO.setAmt(withdrawFundBO.getReqAccAmt());
        subRefundDO.setCcy(withdrawFundBO.getCcy());
        subRefundDO.setStatus(PayStatusEnums.INIT.getCode());
        subRefundDO.setOrigTransDate(withdrawFundBO.getTransDate());
        subRefundDO.setOrigFundDetailNo(withdrawFundBO.getFundDetailNo());
        subRefundDO.setOrigTransReceiptNo(withdrawFundBO.getTransReceiptNo());

        return subRefundDO;
    }

    /**
     * 预授权主单BO转换DO对象
     *
     * @param preAuthBO     主单BO对象
     * @return              主单DO对象
     */
    public static PreAuthDO preAuthBOToDO(PreAuthBO preAuthBO) {
        if (preAuthBO == null) {
            return null;
        }
        PreAuthDO preAuthDO = new PreAuthDO();
        preAuthDO.setRelationNo(preAuthBO.getRelationNo());
        preAuthDO.setPayerNo(preAuthBO.getPayerNo());
        preAuthDO.setOrigPayNo(preAuthBO.getOrigPayNo());
        preAuthDO.setDealAmt(Constant.validateLong(preAuthBO.getDealAmt()));
        preAuthDO.setDepositExtra(preAuthBO.getDepositExtra());
        preAuthDO.setRequestBizNo(preAuthBO.getRequestBizNo());
        preAuthDO.setRequestOrderNo(preAuthBO.getRequestOrderNo());
        preAuthDO.setRequestChannel(preAuthBO.getRequestChannel());
        preAuthDO.setRequestDate(preAuthBO.getRequestDate());
        preAuthDO.setRequestSystem(preAuthBO.getRequestSystem());
        preAuthDO.setPayNo(preAuthBO.getPayNo());
        preAuthDO.setAmt(preAuthBO.getAmt());
        preAuthDO.setCcy(preAuthBO.getCcy());
        preAuthDO.setServiceCode(preAuthBO.getServiceCode());
        preAuthDO.setStatus(preAuthBO.getStatus());
        preAuthDO.setBizType(preAuthBO.getBizType());
        preAuthDO.setSubBizType(preAuthBO.getSubBizType());
        preAuthDO.setCreatedBy(preAuthBO.getOperator());
        preAuthDO.setUpdatedBy(preAuthBO.getOperator());
        preAuthDO.setUsableFlag(UsableFlagEnum.USABLE.getCode());

        return preAuthDO;
    }

    /**
     * 预授权子单BO对象转换DO对象
     *
     * @param preAuthBO     预授权子单BO对象
     * @return              DO对象
     */
    public static List<SubPreAuthDO> subPreAuthBOToDO(PreAuthBO preAuthBO) {
        List<SubPreAuthBO> subPreAuthBOs = preAuthBO.getSubPreAuthBO();
        if (!com.alibaba.dubbo.common.utils.CollectionUtils.isNotEmpty(subPreAuthBOs)) {
            return null;
        }
        List<SubPreAuthDO> subPreAuthDOs = Lists.newArrayList();
        subPreAuthBOs.forEach(subPreAuthBO -> {
            SubPreAuthDO subPreAuthDO = new SubPreAuthDO();
            subPreAuthDO.setFundDetailNo(subPreAuthBO.getFundDetailNo());
            subPreAuthDO.setPayNo(subPreAuthBO.getPayNo());
            subPreAuthDO.setFundType(subPreAuthBO.getFundType());
            subPreAuthDO.setPayerAccNo(subPreAuthBO.getPayerAccNo());
            subPreAuthDO.setPayeeAccNo(subPreAuthBO.getPayeeAccNo());
            subPreAuthDO.setFundingChannel(subPreAuthBO.getFundingChannel());
            subPreAuthDO.setSubFundingChannel(subPreAuthBO.getSubFundingChannel());
            subPreAuthDO.setPayToolType(subPreAuthBO.getPayToolType());
            subPreAuthDO.setPayToolSubType(subPreAuthBO.getPayToolSubType());
            subPreAuthDO.setCcy(subPreAuthBO.getCcy());
            subPreAuthDO.setAmt(subPreAuthBO.getAmt());
            subPreAuthDO.setStatus(subPreAuthBO.getStatus());
            subPreAuthDO.setRelateFundDetailNo(subPreAuthBO.getRelateFundDetailNo());
            subPreAuthDO.setTransReceiptNo(subPreAuthBO.getTransReceiptNo());
            subPreAuthDO.setTransDate(subPreAuthBO.getTransDate());
            subPreAuthDO.setErrorCode(subPreAuthBO.getErrorCode());
            subPreAuthDO.setErrorReason(subPreAuthBO.getErrorReason());
            subPreAuthDO.setFdDtlExtra(subPreAuthBO.getFdDtlExtra());
            subPreAuthDO.setFreezeCode(subPreAuthBO.getFreezeCode());
            subPreAuthDO.setEventNo(subPreAuthBO.getEventNo());
            subPreAuthDO.setFreezeNo(subPreAuthBO.getFreezeNo());
            subPreAuthDO.setUnfreezeReceiptNo(subPreAuthBO.getUnfreezeReceiptNo());
            subPreAuthDO.setDescription(subPreAuthBO.getDescription());
            subPreAuthDO.setCreatedBy(preAuthBO.getOperator());
            subPreAuthDO.setUpdatedBy(preAuthBO.getOperator());
            subPreAuthDOs.add(subPreAuthDO);
        });

        return subPreAuthDOs;
    }

    /**
     * PayBO转换PayDO对象
     *
     * @param payBO     BO对象
     * @return          DO对象
     */
    public static PayDO getPayDOByPayBO(PayBO payBO) {
        if (payBO == null) {
            return null;
        }
        PayDO payDO = new PayDO();
        payDO.setEventNo(payBO.getEventNo());
        payDO.setPayerNo(payBO.getPayerNo());
        payDO.setPayeeNo(payBO.getPayeeNo());
        payDO.setRefundAmt(payBO.getRefundAmt());
        payDO.setStatus(payBO.getStatus());
        payDO.setId(payBO.getId());
        payDO.setPayNo(payBO.getPayNo());
        payDO.setRequestChannel(payBO.getRequestChannel());
        payDO.setRelationNo(payBO.getRelationNo());
        payDO.setRequestDate(payBO.getRequestDate());
        payDO.setRequestSystem(payBO.getRequestSystem());
        payDO.setRequestBizNo(payBO.getRequestBizNo());
        payDO.setRequestOrderNo(payBO.getRequestOrderNo());
        payDO.setCcy(payBO.getCcy());
        payDO.setAmt(payBO.getAmt());
        payDO.setSourceCcy(payBO.getSourceCcy());
        payDO.setSourceAmt(payBO.getSourceAmt());
        payDO.setRate(payBO.getRate());
        payDO.setBizType(payBO.getBizType());
        payDO.setSubBizType(payBO.getSubBizType());
        payDO.setMerchantNo(payBO.getMerchantNo());
        payDO.setPaymentInfo(payBO.getPaymentInfo());
        payDO.setDescription(payBO.getDescription());
        payDO.setCreatedAt(payBO.getCreatedAt());
        payDO.setCreatedBy(payBO.getCreatedBy());
        payDO.setUpdatedAt(payBO.getUpdatedAt());
        payDO.setUpdatedBy(payBO.getUpdatedBy());

        return payDO;
    }

    /**
     * PayBO转换TransferDO对象
     *
     * @param payBO     BO对象
     * @return          TransferDO对象
     */
    public static TransferDO getTransferDOByPayBO(PayBO payBO) {
        if (payBO == null) {
            return null;
        }
        TransferDO transferDO = new TransferDO();
        transferDO.setEventNo(payBO.getEventNo());
        transferDO.setStatus(payBO.getStatus());
        transferDO.setId(payBO.getId());
        transferDO.setPayNo(payBO.getPayNo());
        transferDO.setRequestChannel(payBO.getRequestChannel());
        transferDO.setOldPayNo(payBO.getOldPayNo());
        transferDO.setRelationNo(payBO.getRelationNo());
        transferDO.setRequestDate(payBO.getRequestDate());
        transferDO.setRequestSystem(payBO.getRequestSystem());
        transferDO.setRequestBizNo(payBO.getRequestBizNo());
        transferDO.setRequestOrderNo(payBO.getRequestOrderNo());
        transferDO.setServiceCode(payBO.getServiceCode());
        transferDO.setCcy(payBO.getCcy());
        transferDO.setBizType(payBO.getBizType());
        transferDO.setSubBizType(payBO.getSubBizType());
        transferDO.setDescription(payBO.getDescription());
        transferDO.setCreatedAt(payBO.getCreatedAt());
        transferDO.setCreatedBy(payBO.getCreatedBy());
        transferDO.setUpdatedAt(payBO.getUpdatedAt());
        transferDO.setUpdatedBy(payBO.getUpdatedBy());

        return transferDO;
    }

    /**
     * PayBO对象转换ClearDO对象
     *
     * @param payBO     BO对象
     * @return          ClearDO对象
     */
    public static ClearDO getClearDOByPayBO(PayBO payBO) {
        if (payBO == null) {
            return null;
        }
        ClearDO clearDO = new ClearDO();
        clearDO.setRefundAmt(payBO.getRefundAmt());
        clearDO.setStatus(payBO.getStatus());
        clearDO.setId(payBO.getId());
        clearDO.setPayNo(payBO.getPayNo());
        clearDO.setRequestChannel(payBO.getRequestChannel());
        clearDO.setRelationNo(payBO.getRelationNo());
        clearDO.setRequestDate(payBO.getRequestDate());
        clearDO.setRequestSystem(payBO.getRequestSystem());
        clearDO.setRequestBizNo(payBO.getRequestBizNo());
        clearDO.setRequestOrderNo(payBO.getRequestOrderNo());
        clearDO.setServiceCode(payBO.getServiceCode());
        clearDO.setCcy(payBO.getCcy());
        clearDO.setAmt(payBO.getAmt());
        clearDO.setBizType(payBO.getBizType());
        clearDO.setSubBizType(payBO.getSubBizType());
        clearDO.setMerchantNo(payBO.getMerchantNo());
        clearDO.setPaymentInfo(payBO.getPaymentInfo());
        clearDO.setDescription(payBO.getDescription());
        clearDO.setCreatedAt(payBO.getCreatedAt());
        clearDO.setCreatedBy(payBO.getCreatedBy());
        clearDO.setUpdatedAt(payBO.getUpdatedAt());
        clearDO.setUpdatedBy(payBO.getUpdatedBy());

        return clearDO;
    }

    /**
     * PayBO对象转换PreAuthDO对象
     *
     * @param payBO         BO对象
     * @return              PreAuthDO对象
     */
    public static PreAuthDO getPreAuthDOByPayBO(PayBO payBO) {
        if (payBO == null) {
            return null;
        }
        PreAuthDO preAuthDO = new PreAuthDO();
        preAuthDO.setPayerNo(payBO.getPayerNo());
        preAuthDO.setStatus(payBO.getStatus());
        preAuthDO.setId(payBO.getId());
        preAuthDO.setPayNo(payBO.getPayNo());
        preAuthDO.setRequestChannel(payBO.getRequestChannel());
        preAuthDO.setRelationNo(payBO.getRelationNo());
        preAuthDO.setRequestDate(payBO.getRequestDate());
        preAuthDO.setRequestSystem(payBO.getRequestSystem());
        preAuthDO.setRequestBizNo(payBO.getRequestBizNo());
        preAuthDO.setRequestOrderNo(payBO.getRequestOrderNo());
        preAuthDO.setServiceCode(payBO.getServiceCode());
        preAuthDO.setCcy(payBO.getCcy());
        preAuthDO.setBizType(payBO.getBizType());
        preAuthDO.setSubBizType(payBO.getSubBizType());
        preAuthDO.setDescription(payBO.getDescription());
        preAuthDO.setCreatedAt(payBO.getCreatedAt());
        preAuthDO.setCreatedBy(payBO.getCreatedBy());
        preAuthDO.setUpdatedAt(payBO.getUpdatedAt());
        preAuthDO.setUpdatedBy(payBO.getUpdatedBy());

        return preAuthDO;
    }
    public static WithdrawBO getWithdrawBOByPreAuthBO(PreAuthBO preAuthBO){
        WithdrawBO withdrawBO=new WithdrawBO();
        withdrawBO.setPayNo(preAuthBO.getPayNo());
        withdrawBO.setAccountNo(preAuthBO.getSubPreAuthBO().get(0).getPayerAccNo());
        withdrawBO.setBizType(preAuthBO.getBizType());
        withdrawBO.setMerchantNo(preAuthBO.getMerchantNo());
        withdrawBO.setServiceCode(preAuthBO.getServiceCode());
        withdrawBO.setPayNo(preAuthBO.getPayNo());
        withdrawBO.setRequestChannel(preAuthBO.getRequestChannel());
        withdrawBO.setOldPayNo(preAuthBO.getOldPayNo());
        withdrawBO.setRelationNo(preAuthBO.getRelationNo());
        withdrawBO.setRequestOrderNo(preAuthBO.getRequestOrderNo());
        withdrawBO.setRequestDate(preAuthBO.getRequestDate());
        withdrawBO.setRequestSystem(preAuthBO.getRequestSystem());
        withdrawBO.setRequestBizNo(preAuthBO.getRequestBizNo());
        withdrawBO.setCcy(preAuthBO.getCcy());
        withdrawBO.setAmt(preAuthBO.getAmt());
        withdrawBO.setDescription(preAuthBO.getDescription());
        withdrawBO.setOperator(preAuthBO.getOperator());
        WithdrawFundBO withdrawFundBO=new WithdrawFundBO();
        SubPreAuthBO subPreAuthBO=preAuthBO.getSubPreAuthBO().get(0);
        withdrawFundBO.setPayNo(subPreAuthBO.getPayNo());
        withdrawFundBO.setFundDetailNo(subPreAuthBO.getFundDetailNo());
        withdrawFundBO.setPayToolType(subPreAuthBO.getPayToolType());
        withdrawFundBO.setCcy(subPreAuthBO.getCcy());
        withdrawFundBO.setAmt(subPreAuthBO.getAmt());
        withdrawFundBO.setAccountNo(subPreAuthBO.getPayerAccNo());
        withdrawFundBO.setStatus(subPreAuthBO.getStatus());
        withdrawFundBO.setEventNo(subPreAuthBO.getEventNo());
        withdrawFundBO.setReqAccAmt(subPreAuthBO.getAmt());
        withdrawFundBO.setFundType(subPreAuthBO.getFundType());
        withdrawFundBO.setFreezeCode(subPreAuthBO.getFreezeCode());
        withdrawFundBO.setUnfreezeNo(subPreAuthBO.getFreezeNo());
        withdrawBO.setWithdrawFundBO(Lists.newArrayList(withdrawFundBO));
        return withdrawBO;
    }

    /**
     * 封装提现单
     *
     * @param withdrawBO    提现参数对象
     * @return              提现主单
     */
    public static FreezeDO getFreezeDO(WithdrawBO withdrawBO) {

        WithdrawFundBO freezeFund = withdrawBO.getWithdrawFund(withdrawBO.getWithdrawFundBO(),
                FundTypeEnum.WITHDRAW_FREEZE.getCode());
        if (freezeFund == null) {
            return null;
        }
        FreezeDO freezeDO = new FreezeDO();
        freezeDO.setRelationNo(withdrawBO.getRelationNo());
        freezeDO.setRequestSystem(withdrawBO.getRequestSystem());
        freezeDO.setRequestBizNo(withdrawBO.getRequestBizNo());
        freezeDO.setRequestOrderNo(withdrawBO.getRequestOrderNo());
        freezeDO.setRequestDate(withdrawBO.getRequestDate());
        freezeDO.setFreezeNo(freezeFund.getFundDetailNo());
        freezeDO.setPayNo(withdrawBO.getPayNo());
        freezeDO.setBizType(withdrawBO.getBizType());
        freezeDO.setSubBizType(withdrawBO.getSubBizType());

        freezeDO.setMerchantNo(withdrawBO.getMerchantNo());
        freezeDO.setServiceCode(withdrawBO.getServiceCode());

        freezeDO.setCcy(withdrawBO.getCcy());
        freezeDO.setAmt(withdrawBO.getAmt());
        freezeDO.setDescription(withdrawBO.getDescription());
        freezeDO.setStatus(freezeFund.getStatus());
        freezeDO.setAccountNo(withdrawBO.getAccountNo());
        freezeDO.setFreezeType("DRAW");
        freezeDO.setUpdatedBy(withdrawBO.getOperator());
        freezeDO.setCreatedBy(withdrawBO.getOperator());
        freezeDO.setErrorCode(freezeFund.getErrorCode());
        freezeDO.setErrorReason(freezeFund.getErrorReason());
        freezeDO.setFreezeCode(freezeFund.getFreezeCode());
        return freezeDO;
    }

}
